<template>
  <div>
    <el-dialog
      :title="item.id ? '编辑' : '新增'"
      :visible.sync="editVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="分类：">
          <el-select v-model="form.category_code" style="width: 100%" clearable filterable placeholder="请选择分类">
            <el-option v-for="dist in categoryList" :key="dist.category_code" :label="dist.category_desc" :value="dist.category_code" />
          </el-select>
        </el-form-item>
        <el-form-item label="字典值：">
          <el-input v-model="form.dict_code" clearable placeholder="请输入字典值" />
        </el-form-item>
        <!-- <el-form-item label="值描述：">
          <el-input v-model="form.category_desc" clearable placeholder="请输入" />
        </el-form-item> -->
        <el-form-item label="填报单位：">
          <el-select v-model="form.district_id" style="width: 100%" clearable filterable placeholder="请选择填报单位">
            <el-option v-for="dist in districtList" :key="dist.id" :label="dist.name" :value="dist.id" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="排序：">
          <el-input v-model="form.order" placeholder="请输入" clearable />
        </el-form-item> -->
        <el-form-item v-if="form.category_code == 'StarredFamilyHotel'" label="星级：">
          <el-select v-model="extra3['星级']" style="width: 100%" clearable filterable placeholder="请选择">
            <el-option v-for="dist in starList3" :key="dist.value" :label="dist.name" :value="dist.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredFamilyHotel'" label="客房数：">
          <el-input v-model="extra3['客房数']" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredFamilyHotel'" label="床位数：">
          <el-input v-model="extra3['床位数']" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item v-if="form.category_code == 'ALevelScenicSpot'" label="质量等级：">
          <el-select v-model="extra2['质量等级']" style="width: 100%" clearable filterable placeholder="请选择">
            <el-option v-for="dist in starList2" :key="dist.value" :label="dist.name" :value="dist.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredHotel'" label="星级：">
          <el-select v-model="extra2['星级']" style="width: 100%" clearable filterable placeholder="请选择">
            <el-option v-for="dist in starList" :key="dist.value" :label="dist.name" :value="dist.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredHotel'" label="客房数：">
          <el-input v-model="extra['客房数']" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredHotel'" label="床位数：">
          <el-input v-model="extra['床位数']" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item v-if="form.category_code == 'StarredHotel'" label="备注：">
          <el-input v-model="extra['备注']" clearable placeholder="请输入" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button v-preventReClick="3000" type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { updateDictionary, createDictionary } from '@/api/authority'
export default {
  props: {
    editVisible: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {}
    },
    districtList: {
      type: Array,
      default: () => []
    },
    categoryList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        category_code: '',
        district_id: '',
        dict_code: ''
        // extra: ''
      },
      extra: {
        '备注': '',
        '星级': '',
        '客房数': '',
        '床位数': ''
      },
      extra3: {
        '星级': '',
        '客房数': '',
        '床位数': ''
      },
      extra2: {
        '质量等级': ''
      },
      starList: [
        {
          value: '一星级',
          label: '一星级'
        },
        {
          value: '二星级',
          label: '二星级'
        },
        {
          value: '三星级',
          label: '三星级'
        },
        {
          value: '四星级',
          label: '四星级'
        },
        {
          value: '五星级',
          label: '五星级'
        }
      ],
      starList2: [
        {
          value: 'A',
          label: 'A'
        },
        {
          value: 'AA',
          label: 'AA'
        },
        {
          value: 'AAA',
          label: 'AAA'
        },
        {
          value: 'AAAA',
          label: 'AAAA'
        },
        {
          value: 'AAAAA',
          label: 'AAAAA'
        }
      ],
      starList3: [
        {
          value: '金星',
          label: '金星'
        },
        {
          value: '银星',
          label: '银星'
        },
        {
          value: '铜星',
          label: '铜星'
        }
      ],
      roleData: [],
      fileList: []
    }
  },
  created() {
    if (this.item.id) {
      this.form = { ...this.item }
      if (this.item.extra) {
        this.extra = this.item.extra
        this.extra2 = this.item.extra
        this.extra3 = this.item.extra
      }
    }
  },
  methods: {
    onSubmit() {
      let saveItem = {}
      if (this.form.category_code === 'StarredHotel') {
        saveItem = { district_id: this.form.district_id, dict_code: this.form.dict_code, category_code: this.form.category_code, extra: this.extra, id: this.item.id }
      } else if (this.form.category_code === 'ALevelScenicSpot') {
        saveItem = { district_id: this.form.district_id, dict_code: this.form.dict_code, category_code: this.form.category_code, extra: this.extra2, id: this.item.id }
      } else if (this.form.category_code === 'StarredFamilyHotel') {
        saveItem = { district_id: this.form.district_id, dict_code: this.form.dict_code, category_code: this.form.category_code, extra: this.extra3, id: this.item.id }
      } else {
        saveItem = { district_id: this.form.district_id, dict_code: this.form.dict_code, category_code: this.form.category_code, id: this.item.id }
      }
      if (this.item.id) {
        updateDictionary(saveItem).then(res => {
          if (res.status === 200) {
            this.$emit('changeResult', true)
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          }
        })
      } else {
        createDictionary(saveItem).then(res => {
          if (res.status === 200) {
            this.$emit('changeResult', true)
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          }
        })
      }
    },
    handleClose() {
      this.$emit('changeResult', false)
    }
  }
}
</script>
